﻿/* 
 * @autor: hectorea
 * @date: 03/08/12
 * @project: ReversingWords
 * @reference: 
 */

namespace ReversingWords
{
    internal static class StringExtension
    {
        /// <summary>
        /// Reverse the order of the words
        /// </summary>
        /// <param name="input">Input string</param>
        /// <example>
        /// Input: "I am Hector"
        /// Output: "Hector am I"
        /// </example>
        /// <returns>A string with the words reversed</returns>
        internal static string WordReversing(this string input)
        {
            if (input.Length <= 0)
                return string.Empty;

            string reversed = string.Empty;
            string result = string.Empty;

            for (int i = 0; i < input.Length; i++)
            {
                if (input[i] != ' ')
                {
                    reversed += input[i];
                }
                else
                {
                    result = reversed + input[i] + result;
                    reversed = string.Empty;
                }
            }
            //We need this additional step because in the last iteration
            //the value in reversed isn't assigned to result
            result = reversed + ' ' + result;
            reversed = string.Empty;

            return result;
        }        
        /// <summary>
        /// Reverse the order of the words using an array
        /// </summary>
        /// <param name="input">Input string</param>
        /// <example>
        /// Input: "I am Hector"
        /// Output: "Hector am I"
        /// </example>
        /// <returns>A string with the words reversed</returns>
        internal static string WordReversingWithArray(this string input)
        {
            if (input == string.Empty)
                return string.Empty;

            char[] reversed = new char[input.Length];

            for (int i = 0, j = input.Length - 1; i <= j; i++, j--)
            {
                reversed[i] = input[j];
                reversed[j] = input[i];
            }

            return new string(reversed);
        }
        /// <summary>
        /// Reverse the order of the words using XOR operations
        /// </summary>
        /// <param name="input">Input string</param>
        /// <example>
        /// Input: "I am Hector"
        /// Output: "Hector am I"
        /// </example>
        /// <returns>A string with the words reversed</returns>
        internal static string WordReversingOptimized(this string input)
        {
            if (input == string.Empty)
                return string.Empty;

            char[] reversed = input.ToCharArray();

            for (int i = 0, j = input.Length - 1; i < j; i++, j--)
            {
                reversed[i] ^= reversed[j];
                reversed[j] ^= reversed[i];
                reversed[i] ^= reversed[j];
            }
            return new string(reversed);
        }
    }
}
